<?php
namespace app\admin\command\statistics;

use app\admin\model\user\User;
use app\admin\model\user\Order;
use app\admin\model\agent\Agent;
use app\admin\model\tongji\StatustucsDaySum;
use app\admin\model\tongji\D;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class DayZong extends Command{
    protected function configure()
    {
        $this->setName('day:sum')->setDescription('每日总统计');
    }
    protected function execute(Input $input, Output $output)
    {
        $user_model=new User();
        $order_model=new Order();
        $agent_model=new Agent();
        $statictics_day_model=new StatustucsDaySum();
        $user_num=$user_model->whereTime('create_time','yesterday')->count();
        $order_user_num=$order_model->whereTime('create_time','yesterday')->where('status',1)->group('user_id')->count();
        $order_price=$order_model->whereTime('create_time','yesterday')->where('status',1)->sum('price');
        $old_date=$statictics_day_model->where('date',date('Ymd',strtotime('-1 day')))->find();
        $agent_count=$agent_model->whereTime('create_time','yesterday')->count();


        //记录总数据
        if(!empty($old_date)){
            $old_date->new_user=$user_num;
            $old_date->new_agent=$agent_count;
            $old_date->pay_user_num=$order_user_num;
            $old_date->pay_price=$order_price;
            $old_date->save();
        }else{
            $data=[
                'date'=>date('Ymd',strtotime('-1 day')),
                'new_user'=>$user_num,
                'new_agent'=>$agent_count,
                'pay_user_num'=>$order_user_num,
                'pay_price'=>$order_price,

            ];
            $statictics_day_model->save($data);
        }
    }
}
